package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.mapper.CommuPlatMapper;
import com.srzk.youthleagueback.mapper.ProjectMapper;
import com.srzk.youthleagueback.pojo.CommuPlat;
import com.srzk.youthleagueback.pojo.Meeting;
import com.srzk.youthleagueback.pojo.Project;
import com.srzk.youthleagueback.pojo.SocialPractice;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.utils.LogConfig;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class CommuPlatService {
    @Autowired
    private CommuPlatMapper mapper;

    public Result<Map<String,Long>> add(CommuPlat object) {
        int result = mapper.insert(object);
        if (result == 1) {
            HashMap<String,Long> map = new HashMap<>();
            map.put("id",object.getId());
            return Result.success(map);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<String> delete(String user, String year, Long id) {
        LogConfig.LOG.debug(mapper.selectByPrimaryKey(id ,user, year).toString());
        int result = mapper.deleteByUserAndYearAndTypeAndId(user, year, id);
        if (result == 1) {
            return Result.success("删除成功");
        } else {
            return Result.error(DELETE_FAIL);
        }
    }

    public Result<String> update(CommuPlat object) {
        int result = mapper.updateByPrimaryKey(object);
        if (result == 1) {
            return Result.success("更新成功");
        } else {
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<Map<String,Object>> getList(String user, String year) {
        List<CommuPlat> projects = mapper.selectByPrimaryKeyList(user, year);
        if (projects != null) {
            Map<String,Object> objectMap = new HashMap<>();
            ArrayList<Object> resultList = new ArrayList<>();

            projects.forEach(items->{

                Map<String,Object> info = new HashMap<>();
                info.put("ID",items.getPlatid());
                info.put("type",items.getType());
                info.put("name",items.getName());
                info.put("function",items.getFuction());
                info.put("note",items.getNote());

                Map<String,Object> listItem = new HashMap<>();
                listItem.put("id",items.getId());
                listItem.put("commuPlatformInfo",info);

                resultList.add(listItem);
            });
            objectMap.put("commuPlatformList",resultList);

            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> get(String user, String year, Long id) {
        CommuPlat object = mapper.selectByPrimaryKey(id, user, year);
        if (object != null) {
            Map<String,Object> info = new HashMap<>();
            info.put("type",object.getType());
            info.put("ID",object.getPlatid());
            info.put("name",object.getName());
            info.put("function",object.getFuction());
            info.put("note",object.getNote());

            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("commuPlatformInfo",info);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getGradeList(String grade, String year) {
        List<CommuPlat> allData = mapper.selectByGrade(year);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();

            allData.forEach(items -> {
                List<Map<String, Object>> list = new ArrayList<>();
                if (ObjectUtil.getGrade(items.getUser()).equals(grade)) {
                    String getClass = ObjectUtil.getClass(items.getUser());

                    Map<String,Object> listItem = new HashMap<>();
                    listItem.put("name",items.getName());
                    listItem.put("type",items.getType());
                    listItem.put("platId",items.getPlatid());

                    boolean flag = false;
                    for (Map<String, Object> stringObjectMap : data) {
                        if (getClass.equals(stringObjectMap.get("class"))) {
                            ArrayList tempList = (ArrayList) stringObjectMap.get("list");
                            tempList.add(listItem);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        Map<String, Object> dataItem = new HashMap<>();
                        dataItem.put("class", getClass);
                        list.add(listItem);
                        dataItem.put("list", list);
                        data.add(dataItem);
                    }
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getHistoryList(String user) {
        List<CommuPlat> allData = mapper.selectAll(user);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();
            allData.forEach(items -> {
                ArrayList<Map<String, Object>> list;

                Map<String,Object> listItem = new HashMap<>();
                listItem.put("name",items.getName());
                listItem.put("type",items.getType());
                listItem.put("platId",items.getPlatid());

                boolean flag = false;
                for (Map<String, Object> datum : data) {
                    if(items.getYear().equals(datum.get("year"))){
                        list = (ArrayList<Map<String, Object>>) datum.get("list");
                        list.add(listItem);
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    list = new ArrayList<>();
                    Map<String, Object> dataItem = new HashMap<>();
                    dataItem.put("year", items.getYear());
                    list.add(listItem);
                    dataItem.put("list", list);
                    data.add(dataItem);
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }
}
